
<template>
  <!-- 用户列表 -->
  <div class="users">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="content">
      <div class="content_k1">
        <div class="content_k2">
          <el-input v-model="input" placeholder="请输入内容"></el-input
          ><el-button
            @click="search"
            type="info"
            icon="el-icon-search"
          ></el-button>
        </div>
        <el-button type="primary" @click="add">添加用户</el-button>
      </div>
      <!-- =========================表格======================================= -->
      <el-table
        border
        :data="tableData"
        highlight-current-row
        @current-change="handleCurrentChange1"
        style="width: 100%"
      >
        <el-table-column type="index" width="50" label="#"></el-table-column>
        <el-table-column property="username" label="姓名" width="120">
        </el-table-column>
        <el-table-column property="email" label="邮箱" width="120">
        </el-table-column>
        <el-table-column property="create_time" label="电话"> </el-table-column>
        <el-table-column property="role_name" label="角色"> </el-table-column>
        <el-table-column label="状态"
          ><template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              @change="switchs(scope.$index)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column property="address" label="操作"
          ><template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="show(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="del(scope.row.id)"
            ></el-button>
            <el-button
              type="warning"
              icon="el-icon-setting"
              @click="distribution(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- ======================分页器============================== -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="Data.total"
      >
      </el-pagination>
      <!-- ==========================编辑================================== -->
      <el-dialog title="编辑用户" :visible.sync="dialogFormVisible">
        <el-form :model="item">
          <el-form-item label="用户名" :label-width="formLabelWidth">
            <el-input
              :disabled="true"
              v-model="item.username"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="邮箱" :label-width="formLabelWidth">
            <el-input v-model="item.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="手机" :label-width="formLabelWidth">
            <el-input v-model="item.create_time" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="preservation">保 存</el-button>
        </div>
      </el-dialog>
      <!-- ==========================添加================================== -->
      <el-dialog title="添加用户" :visible.sync="dialogFormVisible1">
        <el-form :model="item_s" :rules="rules">
          <el-form-item
            prop="username"
            label="用户名"
            :label-width="formLabelWidth"
          >
            <el-input v-model="item_s.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            prop="password"
            label="密码"
            :label-width="formLabelWidth"
          >
            <el-input
              type="password"
              v-model="item_s.password"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item prop="email" label="邮箱" :label-width="formLabelWidth">
            <el-input v-model="item_s.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            prop="mobile"
            label="电话"
            :label-width="formLabelWidth"
          >
            <el-input v-model="item_s.mobile" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible1 = false">取 消</el-button>
          <el-button type="primary" @click="adds">确 定</el-button>
        </div>
      </el-dialog>
      <!-- ==========================选择器================================== -->
      <el-dialog title="分配角色" :visible.sync="dialogFormVisible2">
        <div class="admin">当前的用户:{{item_ss.username}}</div>
        <div class="role">当前的角色:{{item_ss.role_name}}</div>
        分配新角色:
        <el-select v-model="value" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.roleName"
            :value="item.id"
          >
          </el-option>
        </el-select>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible2 = false">取 消</el-button>
          <el-button type="primary" @click="distributions">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
 

<script>
import { All } from "../../utils/API";
export default {
  name: "users",
  data() {
    return {
      // 提交到弹出框
      item: {},
      // 打开关闭弹出框
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      // 渲染数据
      Data: {},
      // 显示几条数据
      pagesize: 2,
      // 搜索框
      input: "",
      // 当前页
      currentPage4: 1,
      // 渲染数据
      tableData: [],
      // 像素
      formLabelWidth: "120px",
      // 添加
      item_s: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      // 分配角色
      item_ss: {},
      // 校验
      rules: {
        username: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
        mobile: [
          { required: true, message: "请输入电话号码", trigger: "blur" },
          {
            min: 10,
            max: 11,
            message: "长度在 10 到 11 个字符",
            trigger: "blur",
          },
        ],
      },
      // 选择器
      options: [],
        value: ''
    };
  },
  mounted() {
    this.Refresh();
  },
  methods: {
    handleCurrentChange1(val) {
      this.currentRow = val;
    },
    handleCurrentChange(val) {
      this.currentPage4 = val;
      this.Refresh();
      console.log(`当前页: ${val}`);
    },
    handleSizeChange(val) {
      this.pagesize = val;
      this.Refresh();
      console.log(`每页 ${val} 条`);
    },
    // 一行数据
    show(item) {
      this.item =JSON.parse(JSON.stringify(item));
      this.dialogFormVisible = true;
    },
    // 添加用户
    add() {
      this.dialogFormVisible1 = true;
    },
    // 添加用户二次确定
    async adds() {
      this.dialogFormVisible1 = false;
      let res = await All("/users", this.item_s, "POST");
      console.log(res);
      this.Refresh();
    },
    // 封装刷新接口
    async Refresh() {
      let res = await All("/users", {}, "GET", {
        pagenum: this.currentPage4,
        pagesize: this.pagesize,
      });
      console.log(res);
      this.Data = res.data.data;
      this.tableData = res.data.data.users;
    },
    // 状态
    async switchs(indexs) {
      let item = {};
      this.tableData.forEach((ele, index) => {
        if (index == indexs) {
          item = ele;
        }
      });
      let res = await All(`users/${item.id}/state/${item.mg_state}`, {}, "PUT");
    },
    // 修改并保存用户数据
    async preservation() {
      this.dialogFormVisible = false;
      let res = await All(
        `/users/${this.item.id}`,
        { mobile: this.item.create_time, email: this.item.email },
        "PUT"
      );
      this.Refresh()
    },
    // 删除接口
    async del(id) {
      let res = await All(`/users/${id}`, {}, "delete");
      this.Refresh();
    },
    // 搜索
    async search() {
      // let res = await All(`/users/:id`)
      // console.log(res);
    },
    // 分配角色
    async distribution(item) {
      let res = await All("/roles", {}, "GET");
      this.dialogFormVisible2 = true;
      this.item_ss.username = item.username
      this.item_ss.role_name = item.role_name
      this.item_ss.id = item.id
      console.log(res.data.data);
      this.options = res.data.data
    },
    // 保存分配的角色
   async distributions(){
     this.dialogFormVisible2 = false;
     let res = await All(`/users/${this.item_ss.id}/role`,{rid:this.value},'PUT')
      this.Refresh() 
    }
  },
};
</script>
 
<style lang = "scss" scoped>
.users {
  width: 100%;
  height: 100%;
  background-color: #eaedf1;
  .el-breadcrumb {
    line-height: 50px;
    text-indent: 1em;
  }
  .content {
    width: 95%;
    background-color: white;
    box-sizing: border-box;
    padding: 20px;
    margin: 0 auto;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
  }
  .content_k1 {
    display: flex;
    height: 40px;
  }
  .content_k2 {
    width: 40%;
    display: flex;
    margin-right: 30px;
  }
  .admin,.role{
    margin: 20px 0;
  }
}
</style>